/**
 * User: alesj
 * Date: 2004.8.13
 * Time: 14:01:53
 */

package com.generalynx.ecos.connector.mail.client;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.ui.velocity.VelocityEngineUtils;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Map;

public class MailClient implements IMailClient {

    private static Log logger = LogFactory.getLog(MailClient.class);

    public static final String MIMETYPE_PLAIN = "text/plain; charset=UTF-8";
    public static final String MIMETYPE_HTML = "text/html; charset=UTF-8";

    private JavaMailSender mailSender;
    private VelocityEngine velocityEngine;
    private SimpleMailMessage message; //default message
    private String template; //default template

    //--- mime messages ---

    public MimeMessage createMimeMessage() {
        return mailSender.createMimeMessage();
    }

    public MimeMessage createMimeMessage(String from,
                                        String to,
                                        String subject,
                                        String messageBody) throws MessagingException {
        return createMimeMessage(from, to, subject, messageBody, MIMETYPE_PLAIN);
    }

    public MimeMessage createMimeMessage(String from,
                                         String to,
                                         String subject,
                                         String messageBody,
                                         String contentType) throws MessagingException {
        MimeMessage msg = createMimeMessage();
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject(subject, "utf-8");
        msg.setContent(messageBody, contentType);
        return msg;
    }

    public void sendMail(MimeMessage msg) {
        mailSender.send(msg);
    }

    //--- simple msg ---

    public void sendMail(String sendTo, Map model) {
        sendMail(new String[]{sendTo}, model);
    }

    public void sendMail(String sendTo, Map model, String template) {
        sendMail(new String[]{sendTo}, model, template);
    }

    public void sendMail(String[] sendTo, Map model) {
        sendMail(sendTo, model, template);
    }

    public void sendMail(String[] sendTo, Map model, String template) {
        SimpleMailMessage msg = new SimpleMailMessage(this.message);
        msg.setTo(sendTo);

        String result = null;
        try {
            // template must be in classpath
            result = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, template, model);
        } catch (VelocityException e) {
            logger.error(e.getMessage());
        }
        msg.setText(result);
        try {
            mailSender.send(msg);
        } catch (MailException ex) {
            logger.error(ex.getMessage());
        }
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setMessage(SimpleMailMessage message) {
        this.message = message;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
    }

}
